/**
 * kaGridEndButton 封装
 * 
 * 版本：v1.0
 * 
 * Stanley
 **/
layui.define(['jquery'],
    function (exports) {
        var kaTree = new function () {
            this.init = function (params) {
                top.kaFindTreeAndDataGridTreeInputParams = {};
                top.kaFindTreeAndDataGridTreeInputParams[params.id] = params;
            }
            this.getHtml = function (id, filterName) {
                return `
                    <div class="layui-input-group" style="width: 100%;margin-top: 10px;margin-bottom: 10px;">
                        <input id="find` + id + `TreeInput" filterName="` + filterName + `" type="text" placeholder="请搜索" class="layui-input">
                        <div class="layui-input-split layui-input-suffix" onclick="layui.kaTree.clear('treeAndDataGridTree')" style="cursor: pointer;">
                            <i class="layui-icon layui-icon-refresh"></i>
                        </div>
                    </div>
                    <div id="` + id + `"></div>
                `
            }
            this.getData = function (params) { // 初始化
                if (top.kaFindTreeAndDataGridTreeInputParams[params.id]["data"] == undefined || top.kaFindTreeAndDataGridTreeInputParams[params.id]["data"] == null) {
                    var kaAjaxFormData = {};
                    layui.kaAjax.ajax(layui.kaBgUrl.url(params.url), function (rs) {
                        if (rs.code == 200) {
                            data = rs.data;
                            top.kaFindTreeAndDataGridTreeInputParams[params.id]["data"] = data;
                        }
                    }, kaAjaxFormData);
                    return layui.kaTree.filter(params.id, data);
                } else {
                    return layui.kaTree.filter(params.id, top.kaFindTreeAndDataGridTreeInputParams[params.id]["data"]);
                }
            }
            this.filter = function (id, data) {
                var value = layui.$("#find" + id + "TreeInput").val();
                var filterName = layui.$("#find" + id + "TreeInput").attr("filterName");
                return layui.kaTree.filterData(data, value, filterName);
            }
            this.filterData = function (data, value, filterName) {
                if(value != undefined && value != null && value != ""){
                    var filterDatas = [];
                    for (var i in data) {
                        if(data[i][filterName].indexOf(value) > -1){
                            if(data[i].children != undefined && data[i].children != null){
                                data[i].children = this.filterData(data[i].children, value, filterName)
                            }
                            filterDatas.push(data[i]);
                        } else {
                            if(data[i].children != undefined && data[i].children != null){
                                var children = this.filterData(data[i].children, value, filterName)
                                for (var j in children) {
                                    filterDatas.push(children[j]);
                                }
                            }
                        }
                    }
                    return filterDatas;
                } else {
                    return data;
                }
            }
            this.render = function (id) {
                var params =  top.kaFindTreeAndDataGridTreeInputParams[id];
                var data = layui.kaTree.getData(params);
                layui.tree.render({
                    elem: '#' + params.id,
                    data: data,
                    onlyIconControl: true,  // 是否仅允许节点左侧图标控制展开收缩
                    customName: params.customName,
                    kjQueryClick: params.kjQueryClick,
                    click: params.click
                });
            }
            this.clear = function (id) {
                var params = top.kaFindTreeAndDataGridTreeInputParams[id];
                delete params.data;
                layui.kaTree.render('treeAndDataGridTree');
                layui.$(".QuerySuoDing_3").remove();
                layui.kaGridSearch.init('kaDataGridId');
            }
        };
        exports('kaTree', kaTree);
    }
)